/**
 * @Description: 插件封装
 * @Author: wfy
 * @Date: 2019/3/11 9:30 AM
 */

var ebs = window.ebs || {
    webUploader: {
        init: function (option) {
            var $list = $('#thelist'),
                // $btn = $('#ctlBtn'),
                state = 'pending';
            var uploader = WebUploader.create({
                // swf文件路径
                swf: '/assets/js/webuploader/Uploader.swf',
                // 文件接收服务端。
                server: '/api/uploadAttachment',
                // 选择文件的按钮。可选。
                // 内部根据当前运行是创建，可能是input元素，也可能是flash.
                pick: '#picker',
                // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
                resize: false,
                auto: true,
                formData: {
                    isAttachment: true
                },
                fileNumLimit: 3
            });
            // 当有文件被添加进队列的时候
            uploader.on('fileQueued', function (file) {
                // var $list = $('#thelist');
                var fileDes = '<div id="' + file.id + '" class="item">' +
                    '<h4 class="info">' + file.name + '</h4>' +
                    '<a id="delete-file" class="state" onclick="deleteFileFromList(this)">删除</a>';
                if (file.source.id) {
                    fileDes += '<input id="file-' + file.source.id + '" type="hidden" name="fileIds" value="' + file.source.id + '"/>';
                }
                fileDes += '</div>';
                $list.append(fileDes);
            });
            // 文件上传过程中创建进度条实时显示。
            uploader.on('uploadProgress', function (file, percentage) {
                var $li = $('#' + file.id),
                    $percent = $li.find('.progress .progress-bar');

                // 避免重复创建
                if (!$percent.length) {
                    $percent = $('<div class="progress progress-striped active">' +
                        '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                        '</div>' +
                        '</div>').appendTo($li).find('.progress-bar');
                }

                $li.find('a.state').text('上传中');

                $percent.css('width', percentage * 100 + '%');
            });
            uploader.on('uploadSuccess', function (file, response) {
                $('#' + file.id).find('a.state').text('删除');
                var fileId = response.fileIdArray[0];
                $('#' + file.id).find('a.state').after('<input id="file-' + fileId + '" type="hidden" name="fileIds" value="' + fileId + '"/>');
                console.log(uploader.getFiles());
            });

            uploader.on('uploadError', function (file) {
                $('#' + file.id).find('a.state').text('上传出错');
            });

            uploader.on('uploadComplete', function (file) {
                $('#' + file.id).find('.progress').fadeOut();
            });

            function deleteFileFromList(obj) {
                var $item = $(obj).parent();
                var fileId = $item.attr('id');
                uploader.removeFile(fileId, true);
                $item.remove();
            }

            return uploader;
        }
    }


}